UIGiftVip = Class("UIGiftVip", UIComponentBase)

function UIGiftVip:OnCreate(id, parent)
    self.activeId = id
    self.parent = parent
    self.drawWrapper = GoWrapper.New()
    self.lihui = self:GetChild("lihui");
    self.lihui2 = self:GetChild("lihui2");
    self.banner = self:GetChild("banner")
    self.bannerChar = self:GetChild("bannerChar")
    --self.titlePic = self:GetChild("activitytips")
    self.title = self:GetChild("title")
    self.buttonPrivilege = self:GetChild("buttonPrivilege")
    self.buttonPrivilegeRedDot = self:GetChild("UIRedDots", self.buttonPrivilege)
    --self.vipNum = self:GetChild("vipNum")
    self.vipIcon = self:GetChild("vipIcon")
    self.vipLevel = self:GetChild("vipLevel")
    self.vipProgress =  self.vipLevel:GetChild("num");
    self.vipTips = self:GetChild("vipTips")
    self.giftList = self:GetChild("giftList")
    self.isVipMaxController = self:GetController("isVipMax");
    self.isShowVipController = self:GetController("isShowVip");
    self.isShowVipController.selectedIndex = 1;

    -- 帮助按钮
    self.helpVip = self:GetChild("helpVip")

    self:RegisterOnClick(self.buttonPrivilege,self.ClickVip);
    self.parent:RegisterEvent(Config.EventType.VIPInfoUpdate,self.initVip, self);

    if self.fguiObj then
        self.fguiObj.onTouchBegin:Add(self.OnTouchView, self)
    end
end

function UIGiftVip:Refresh()
    --logicMgr.CostManager.updateActiveOpenStatus(self.activeId);
    self.severData, self.itemData, self.modelTableData = logicMgr.CostManager.getCostData(self.activeId);
    logicMgr.CostManager.updateCostHelp(self, self:GetChild("help"), self.modelTableData);
    --self.titlePic.url = tostring(self.modelTableData.f_TitlePic);
    self.bannerChar.url = tostring(self.modelTableData.f_BannerHead);
    self.banner.url = tostring(self.modelTableData.f_Banner);
    self.title.text = getLanguage(self.modelTableData.f_Title);
    --self.helptxtTips.text = getLanguage(self.modelTableData.f_ExplainText);
    --self.titleTips.text = getLanguage(self.modelTableData.f_ExplainTitle);
    self:initVip();
    self.giftList:RemoveChildrenToPool();
    for k, v in pairs(self.severData.items) do
        local tableData = self.itemData[tostring(v.subId)];
        local item = self.giftList:AddItemFromPool()
        self:updateItem(item, k, v, tableData);
    end
    self.parent:updateCostShowByTableOrJson(self.modelTableData.f_ResourceShow);
    --self.tipsController.selectedIndex = 0;
    logicMgr.CostManager.updateLiHui(self, self.drawWrapper, self.lihui, self.lihui2, self.modelTableData.f_HeroSpine)
end

function UIGiftVip:updateItem(itemObj, index, serverData, tableData)
    local icon = itemObj:GetChild("icon");
    local title = itemObj:GetChild("title");
    local discount = itemObj:GetChild("discount");
    local buy = itemObj:GetChild("buy");
    local titleDiscount = itemObj:GetChild("titleDiscount");
    local discountController = itemObj:GetController("discount");
    title.text = getLanguage(tableData.f_Name);
    --local singleItemConfig;
    --local reward = decodeJsonStrToTabel(tableData.f_Rewards);
    --for k, v in pairs(reward) do
    --    singleItemConfig = logicMgr.ItemManager.getItemConstDatabyModelId(v[1]);
    --    title.text = title.text..v[2]..singleItemConfig.name.." ";
    --end
    if serverData.buyCount <= 0 and tableData.f_AdditionalReward ~= nil and tableData.f_AdditionalReward ~= "" then
        discountController.selectedIndex = 1;
        local giveReward = decodeJsonStrToTabel(tableData.f_AdditionalReward);
        --titleDiscount.text = "+";
        for k, v in pairs(giveReward) do
            --singleItemConfig = logicMgr.ItemManager.getItemConstDatabyModelId(v[1]);
            --titleDiscount.text = titleDiscount.text..v[2]..singleItemConfig.name.." ";
            titleDiscount.text = getLanguage(tableData.f_FirstDesc);
        end
        --discount.text = math.floor((giveReward[1][2] / reward[1][2] + 1) * 100)  .. "%";
        discount.text = getLanguage(tableData.f_Desc);
    else
        discountController.selectedIndex = 0;
    end
    --icon.url = "ui://UIGiftVip/"..tableData.f_Icon;
    icon.url = EnumConst.AtlasPath.ICONAtlas..tableData.f_Icon;
    local itemId = serverData.subId;
    if tableData.f_Price == 0 then
        buy.title = getLanguage("FreeMoney");
    else
        buy.title = "￥"..tableData.f_Price;
    end
    buy.onClick:Clear();
    buy.onClick:Add(function() self:ClickBuy(itemId, tableData.f_ProductId, 1, tableData.f_Price); end);
end

function UIGiftVip:initVip()
    local nowLv, progress, limit, nextLv = logicMgr.VipManager.GetVipInfo();
    local vipExp = logicMgr.ItemManager.getItemConstDatabyModelId(CurrencyEnum.VipExp);
    if nextLv == nil then
        self.isVipMaxController.selectedIndex = 1;
        self.vipLevel.value = 100;
        self.vipProgress.text = progress.."/"..limit;
        self.vipTips.text = getLanguage("Viptext1002");
    else
        self.vipProgress.text = progress.."/"..limit;
        self.vipLevel.value = progress / limit * 100;
        --self.vipNum.text = limit - progress;
        self.vipTips.text = string.format(getLanguage(self.modelTableData.f_VipTip,nil,"获得%d到达%d级光耀圣使"), limit - progress, nextLv)
        if vipExp ~= nil then
            self.vipIcon.url = EnumConst.AtlasPath.ItemAtlas..vipExp["icon"];
        end
        self.isVipMaxController.selectedIndex = 0;
    end
    self.buttonPrivilege.title = nowLv;
    self.buttonPrivilegeRedDot.visible = logicMgr.VipManager.GetVipIsHave();
end

function UIGiftVip:ClickVip()
    showUI(PanelNames.UIGiftVipDetail, nil, self.activeId);
    --self.tipsController.selectedIndex = 2;
    --self:updateVipItem();
end
--
--function UIGiftVip:updateVipItem()
--    local data, isReward = logicMgr.VipManager.GetVipTableDataByLv(self.nowVipLv);
--    if data == nil then
--        UISysTips.AddMsg("数据错误");
--        self:ClickCloseTips();
--        return ;
--    end
--    local nowLv, progress, limit, nextLv = logicMgr.VipManager.GetVipInfo();
--    self.vipTipsTitle.text = string.format("%d级VIP奖励", data.f_VipLevel);
--    self.vipTipsPrivilegeTitle.text = string.format("%d级VIP特权", data.f_VipLevel);
--    if isReward == false and self.nowVipLv <= nowLv and data.f_VipReward ~= "" then
--        self.rewardPrivilege.selectedIndex = 1;
--    else
--        self.rewardPrivilege.selectedIndex = 0;
--    end
--    if data.f_VipReward ~= "" then
--        logicMgr.CostManager.RefreshItemList(self.vipTipsTitleRewardList, decodeJsonStrToTabel(data.f_VipReward), 4);
--    else
--        self.vipTipsTitleRewardList:RemoveChildrenToPool();
--    end
--    self.vipTipsPrivilegeList:RemoveChildrenToPool();
--    if data.f_VipBuff ~= "" then
--        for k, v in pairs(decodeJsonStrToTabel(data.f_VipBuff)) do
--            local item = self.vipTipsPrivilegeList:AddItemFromPool();
--            local itemInfo = item:GetChild("info");
--            local buff = G.dataTable["t_ecobuff"][tostring(v)];
--            if buff ~= nil then
--                itemInfo.text = getLanguage(buff.f_BuffDesc);
--            else
--                itemInfo.text = "";
--            end
--        end
--    end
--    if data.f_VipFuncSwitchDesc ~= "" then
--        for k, v in pairs(decodeJsonStrToTabel(data.f_VipFuncSwitchDesc)) do
--            local item = self.vipTipsPrivilegeList:AddItemFromPool();
--            local itemInfo = item:GetChild("info");
--            itemInfo.text = getLanguage(v, nil , "");
--        end
--    end
--    data, isReward = logicMgr.VipManager.GetVipTableDataByLv(self.nowVipLv + 1);
--    self.vipTipsVipRight.visible = data ~= nil;
--    data, isReward = logicMgr.VipManager.GetVipTableDataByLv(self.nowVipLv - 1);
--    self.vipTipsVipLeft.visible = data ~= nil;
--end
--
--function UIGiftVip:ClickVipRight()
--    self.nowVipLv = self.nowVipLv + 1;
--    self:updateVipItem();
--end
--
--function UIGiftVip:ClickVipLeft()
--    self.nowVipLv = self.nowVipLv - 1
--    self:updateVipItem();
--end
--
--function UIGiftVip:ClickHelp()
--    --self.tipsController.selectedIndex = 1;
--    logicMgr.UIShowManager.ShowCommonTips(self.modelTableData.f_ExplainTitle, self.modelTableData.f_ExplainText);
--end

--function UIGiftVip:ClickCloseTips()
--    self.tipsController.selectedIndex = 0;
--end

function UIGiftVip:ClickBuy(id, payId, num, money)
    logicMgr.CostManager.getRewardByIdAndIndex(self.activeId, id, payId, num, money, self.onBuySuccess, self);
end
--
--function UIGiftVip:ClickReceiveVipReward()
--    logicMgr.VipManager.getVipDrawReward(self.nowVipLv, self.onReceiveVipRewardSuccess, self);
--end
--
--function UIGiftVip:onReceiveVipRewardSuccess()
--    self:updateVipItem();
--end

function UIGiftVip:onBuySuccess()
    --UISysTips.AddMsg("购买成功");
end

-- 点击面板（任意区域）
function UIGiftVip:OnTouchView(evt)
    if self.fguiObj == nil then
        return
    end

    local rect = {
        x = self.helpVip.x,
        y = self.helpVip.y,
        width = self.helpVip.width,
        height = self.helpVip.height
    }
    local point = self.fguiObj:GlobalToLocal(Vector2(evt.inputEvent.x, evt.inputEvent.y))
    
    -- 点击区域为按钮所在位置
    if hitTestPoint(rect, point) then
        return
    end

    self:OnHide()
end

-- 隐藏面板
function UIGiftVip:OnHide()
    -- 界面关闭时隐藏帮助面板
    if self.helpVip and self.helpVip.selected then
        self.helpVip.selected = false
    end
end

-- 卸载
function UIGiftVip:OnDestroy()
    self:OnHide()
    
    if self.fguiObj then
        self.fguiObj.onTouchBegin:Clear()
    end

    if self.RoleLihui then
        destroyImmediate(self.RoleLihui)
        self.RoleLihui = nil
    end
    if self._package and UIPackage.GetById(self._package) then
        UIPackage.RemovePackage(self._package)
    end
end